package com.zrrd.jt_admin.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.jt_admin.common.bo.GoodsBo;
import com.zrrd.jt_admin.common.entity.Goods;
import com.zrrd.jt_admin.common.entity.OrderItem;
import com.zrrd.jt_admin.common.vo.OrderItemVo;
import com.zrrd.jt_admin.order.mapper.OrderItemMapper;
import com.zrrd.jt_admin.order.service.OrderItemService;
import com.zrrd.jt_admin.shop.goods.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-07-11
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {

    @Resource
    private GoodsService goodsService;
    /**
     * 根据orderid查询订单项
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderItemVo> findByOrderId(Integer orderId) {
        List<OrderItem> orderItemList = lambdaQuery().eq(OrderItem::getOrderId, orderId).list();
        List<OrderItemVo> orderItemVoList = orderItemList.stream().map(orderItem -> {
            OrderItemVo orderItemVo = new OrderItemVo();
            BeanUtils.copyProperties(orderItem, orderItemVo);
            Integer goodsId = orderItem.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            GoodsBo goodsBo = new GoodsBo();
            BeanUtils.copyProperties(goods, goodsBo);
            orderItemVo.setGoodsItem(goodsBo);
            return orderItemVo;
        }).collect(Collectors.toList());
        return orderItemVoList;
    }
}
